﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace PIT.Labs.SharePoint.Extensions
{
    /// <summary>
    /// This class contains "<a href="http://msdn.microsoft.com/en-us/library/bb383977%28v=vs.90%29.aspx">Extension Methods</a>" for the <see cref="T:Microsoft.SharePoint.SPListItem"/> class.
    /// </summary>
    /// <seealso cref="T:Microsoft.SharePoint.SPListItem"/>
    public static class SPListItemExtension
    {

        /// <summary>
        /// Creates unique role assignments for the object, optionally copies role assignments from the parent, and optionally clears unique role assignments from descendants. Allows updates via HTTP-GET (Caution: XSS-Attacks)!
        /// </summary>
        /// <param name="spListItem">Represents a SharePoint Foundation list item.</param>
        /// <param name="copyRoleAssignments">
        /// <see cref="Boolean"/><br />
        /// <b>true</b> to copy role assignments from the parent object to this object; <b>false</b> to assign only the current user to security roles.
        /// </param>
        /// <param name="clearSubscopes">
        /// <see cref="Boolean"/><br />
        /// <b>true</b> to clear the unique role assignments from child objects, which in turn inherit role assignments from this object; <b>false</b> to keep the unique role assignments defined on child objects in place.
        /// </param>
        /// <remarks>
        /// This method replaces the default role assignments that the securable object inherits from its parent object with unique role assignments.
        /// </remarks>
        [CLSCompliantAttribute(false)]
        public static void UnsafeBreakRoleInheritance(this SPListItem spListItem, bool copyRoleAssignments, bool clearSubscopes)
        {
            if (spListItem != null)
            {
                bool oldState = spListItem.ParentList.ParentWeb.AllowUnsafeUpdates;
                spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = true;
                spListItem.BreakRoleInheritance(copyRoleAssignments, clearSubscopes);
                spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = oldState;
            }
        }
        
        /// <summary>
        /// Creates unique role assignments for the object and optionally copies role assignments from the parent. Allows updates via HTTP-GET (Caution: XSS-Attacks)!
        /// </summary>
        /// <param name="spListItem">Represents a SharePoint Foundation list item.</param>
        /// <param name="copyRoleAssignments">
        /// <see cref="Boolean"/><br />
        /// <b>true</b> to copy role assignments from the parent object to this object; <b>false</b> to assign only the current user to security roles.
        /// </param>
        [CLSCompliantAttribute(false)]
        public static void UnsafeBreakRoleInheritance(this SPListItem spListItem, bool copyRoleAssignments)
        {
            if (spListItem != null)
            {
                bool oldState = spListItem.ParentList.ParentWeb.AllowUnsafeUpdates;
                spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = true;
                spListItem.BreakRoleInheritance(copyRoleAssignments);
                spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = oldState;
            }
        }

        /// <summary>
        /// Removes the local role assignments and reverts to role assignments from the parent object. Allows updates via HTTP-GET (Caution: XSS-Attacks)!
        /// </summary>
        /// <param name="spListItem">Represents a SharePoint Foundation list item.</param>
        [CLSCompliantAttribute(false)]
        public static void UnsafeResetRoleInheritance(this SPListItem spListItem)
        {
            if (spListItem != null)
            {
                bool oldState = spListItem.ParentList.ParentWeb.AllowUnsafeUpdates;
                spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = true;
                spListItem.ResetRoleInheritance();
                spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = oldState;
            }
        }

        /// <summary>
        /// Updates the list item with changes that are made to the list item. Allows updates via HTTP-GET (Caution: XSS-Attacks)!
        /// </summary>
        /// <param name="spListItem">Represents a SharePoint Foundation list item.</param>
        [CLSCompliantAttribute(false)]
        public static void UnsafeUpdate(this SPListItem spListItem)
        {
            if (spListItem != null)
            {
                if (spListItem.ParentList != null)
                    if (spListItem.ParentList.ParentWeb != null)
                {
                    bool oldState = spListItem.ParentList.ParentWeb.AllowUnsafeUpdates;
                    spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = true;
                    spListItem.Update();
                    spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = oldState;
                }
            }
        }

        /// <summary>
        /// Updates the list item with changes that are made to the list item (uses the system user). Allows updates via HTTP-GET (Caution: XSS-Attacks)!
        /// </summary>
        /// <param name="spListItem">Represents a SharePoint Foundation list item.</param>
        [CLSCompliantAttribute(false)]
        public static void UnsafeSystemUpdate(this SPListItem spListItem)
        {
            if (spListItem != null)
            {
                if (spListItem.ParentList != null)
                    if (spListItem.ParentList.ParentWeb != null)
                    {
                        bool oldState = spListItem.ParentList.ParentWeb.AllowUnsafeUpdates;
                        spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = true;
                        spListItem.SystemUpdate();
                        spListItem.ParentList.ParentWeb.AllowUnsafeUpdates = oldState;
                    }
            }
        }

    }
}
